#include"drv_time.h"
void drv_time_7_init()
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//采样
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Prescaler=8400-1;//预分配系数
	TIM_TimeBaseInitStruct.TIM_Period=10000-1;//装载值
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;//高级定时器专属
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,ENABLE);
	TIM_TimeBaseInit(TIM7, &TIM_TimeBaseInitStruct);

	TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE);

	NVIC_InitStruct.NVIC_IRQChannel=TIM7_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Init(&NVIC_InitStruct);
	TIM_Cmd(TIM7, ENABLE);
}
void drv_time13_init()
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;
	 TIM_OCInitTypeDef  TIM_OCInitStruct;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13,ENABLE);
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOF,&GPIO_InitStruct);
	GPIO_PinAFConfig(GPIOF,GPIO_PinSource8,GPIO_AF_TIM13);//复用
	
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Prescaler=84-1;//整动一次需要的时间
	TIM_TimeBaseInitStruct.TIM_Period=1000-1;//整个pwm的周期
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIM13,&TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM13,TIM_IT_Update, ENABLE);
	
	
	TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;//PW1模式
	TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//
	TIM_OCInitStruct.TIM_Pulse=0;
	TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;//低电平有效
	TIM_OC1Init(TIM13,&TIM_OCInitStruct);
	
	TIM_OC1PreloadConfig(TIM13,TIM_OCPreload_Enable);
	TIM_ARRPreloadConfig(TIM13,ENABLE);
	TIM_Cmd(TIM13,ENABLE);//启动
}
void drv_time2_init()
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;
	 TIM_OCInitTypeDef  TIM_OCInitStruct;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_15;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource15,GPIO_AF_TIM2);//复用
	
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//默认0次分频
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Prescaler=84-1;//整动一次需要的时间
	TIM_TimeBaseInitStruct.TIM_Period=1000-1;//整个pwm的周期
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
	TIM_ITConfig(TIM2,TIM_IT_Update, ENABLE);
	
	
	TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;//PW1模式
	TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//
	TIM_OCInitStruct.TIM_Pulse=0;//初始占空比
	TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//低电平有效
	TIM_OC1Init(TIM2,&TIM_OCInitStruct);
	
	TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
	TIM_CtrlPWMOutputs(TIM2,ENABLE);
	TIM_ARRPreloadConfig(TIM2,ENABLE);
	TIM_Cmd(TIM2,ENABLE);//启动
}

//void set_pwm(uint16_t num)
//{
//		static uint16_t n=0;
//		if(n==0)
//		{
//			num++;	
//		}
//		else
//		{
//			num--;
//		}
//		if(num==900)
//		{
//			n=1;
//		}
//		if(num==0)
//		{
//			n=0;
//		}
//		TIM_SetCompare1(TIM13,num);
//}
